using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace TripTracker
{
    class GPSExport
    {
        private String importfilename;
        private String exportfilename;
        private String[] lineColors = new string[] { "redLine", "yellowLine", "greenLine", "blueLine" };

        public GPSExport()
        {
            importfilename = "tripTrack.txt";
            exportfilename = "tripTrack.kml";
        }

        /// <summary>
        /// Build the .kml file
        /// </summary>
        public void gpsExport()
        {
            try
            {
                if(!File.Exists(importfilename))
                {
                    MessageBox.Show("No Trip found!");
                    return;
                }

                if (File.Exists(exportfilename))
                {
                    DialogResult result = MessageBox.Show("Do you want to overwrite the existing .kml file?\nNo will abort the operation.", "Overwrite file?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        
                    switch (result){
                        case DialogResult.Yes:
                            File.Delete(exportfilename);
                            break;
                        case DialogResult.No:
                            return;
                    }
                }

                StreamReader re = new StreamReader(importfilename);

                FileStream fs = new FileStream(exportfilename, FileMode.CreateNew, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);

                //write .kml specification
                sw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n" +
                            "<Document>\n" +
                            "<name>tripTrack</name>\n" +
                            "<Style id=\"yellowLine\">\n" +
                            "  <LineStyle>\n" +
                            "    <color>7f00ffff</color>\n" +
                            "    <width>4</width>\n" +
                            "  </LineStyle>\n" +
                            "</Style>\n" +
                            "<Style id=\"blueLine\">\n" +
                            "  <LineStyle>\n" +
                            "    <color>7fff0000</color>\n" +
                            "    <width>4</width>\n" +
                            "  </LineStyle>\n" +
                            "</Style>\n" +
                            "<Style id=\"greenLine\">\n" +
                            "  <LineStyle>\n" +
                            "    <color>7f00ff00</color>\n" +
                            "    <width>4</width>\n" +
                            "  </LineStyle>\n" +
                            "</Style>\n" +
                            "<Style id=\"redLine\">\n" +
                            "  <LineStyle>\n" +
                            "    <color>7f0000ff</color>\n" +
                            "    <width>4</width>\n" +
                            "  </LineStyle>\n" +
                            "</Style>\n");
                
                String input = null;
                String previousCellID = null;
                String previousLAI = null;
                bool endLine = false;
                int colorIndex = lineColors.Length - 1;
                while ((input = re.ReadLine()) != null)
                { 
                    String[] data = input.Split('|');

                    if (previousCellID != null && previousLAI != null && previousCellID.Equals(data[5]) && previousLAI.Equals(data[4]))
                    {
                        sw.WriteLine(data[1] + "," + data[0] + "," + data[2]);
                    }
                    else
                    {
                        if (endLine)
                        {
                            sw.Write("</coordinates></LineString>\n" +
                                     "</Placemark>\n");
                        }

                        sw.Write("<Placemark><name>LAI " + data[4] + " CellID " + data[5] + "</name>\n" +
                                  "<styleUrl>#" + lineColors.GetValue(colorIndex) +"</styleUrl>\n" +
                                  "<LineString><coordinates>\n");

                        //longitude,latitude,altitude
                        sw.WriteLine(data[1] + "," + data[0] + "," + data[2]);
                        endLine = true;
                        colorIndex = (++colorIndex) % lineColors.Length;
                    }
                   
                    previousLAI = data[4];
                    previousCellID = data[5];
                }
                
                sw.Write("</coordinates></LineString>\n" +
                         "</Placemark>\n");

                //end .kml specification
                sw.Write("</Document>\n" +
                            "</kml>");

                re.Close();
                sw.Close();
                MessageBox.Show("Export completed!");
            }
            catch (Exception e)
            {
                MessageBox.Show("Warning! " + e.Message);
            }

        }
 
    }
}
